package com.icebergteam.warfacewidget;

import java.io.File;
import java.util.Arrays;
import java.util.Random;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import com.icebergteam.warfacewidget.core.Database;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.OnTaskCompleted;
import com.icebergteam.warfacewidget.core.utils.FilesUtils;
import com.icebergteam.warfacewidget.core.Parser;
import com.icebergteam.warfacewidget.news.data.NewsParser;
import com.icebergteam.warfacewidget.news.ui.NewsActivity;
import com.icebergteam.warfacewidget.news.ui.NewsListViewService;
import com.icebergteam.warfacewidget.news.ui.NewsRequestTask;
import com.icebergteam.warfacewidget.rank.ui.Top100ListViewService;
import com.icebergteam.warfacewidget.rank.ui.Top100RequestTask;
import com.icebergteam.warfacewidget.ui.AboutActivity;
import com.icebergteam.warfacewidget.ui.RatingActivity;
import com.icebergteam.warfacewidget.ui.YouTubeVideoPlayerActivity;
import com.icebergteam.warfacewidget.ui.configuration.ConfigurationActivity;
import com.icebergteam.warfacewidget.videos.data.VideosParser;
import com.icebergteam.warfacewidget.videos.ui.VideosListViewService;
import com.icebergteam.warfacewidget.videos.ui.VideosRequestTask;
import com.icebergteam.warfacewidget.wiki.data.WikiWidgetRequestTask;
import com.icebergteam.warfacewidget.wiki.ui.WikiActivity;
import com.icebergteam.warfacewidget.wiki.ui.WikiListViewService;

public class WidgetProvider extends AppWidgetProvider implements
		OnTaskCompleted {
	public static String className = WidgetProvider.class.getCanonicalName();
	public static String UPDATE_ACTION = String.format("%s.UPDATE_ACTION",
			className);
	public static String RESTORE_ACTION = String.format("%s.RESTORE_ACTION",
			className);
	public static String SETTINGS_ACTION = String.format("%s.SETTINGS_ACTION",
			className);
	public static String INFO_ACTION = String.format("%s.INFO_ACTION",
			className);
	public static String TOP100_LIST_ACTION = String.format("%s.TOP100_LIST_ACTION",
			className);
	public static String NEWS_LIST_ACTION = String.format("%s.NEWS_LIST_ACTION",
			className);
	public static String VIDEOS_LIST_ACTION = String.format("%s.VIDEOS_LIST_ACTION",
			className);
	public static String MORE_INFO_RATING_ACTION = String.format("%s.MORE_INFO_RATING_ACTION",
			className);
	public static String WIKI_ACTION = String.format("%s.WIKI_ACTION",
			className);

	public static final String ROOT_PATH = String.format("%s/Android/data/%s",
			/*Environment.getExternalStorageDirectory()*/"/mnt/sdcard", WidgetProvider.class
					.getPackage().getName());
	
	public static final String CACHE_PATH = String.format("%s/cache", WidgetProvider.ROOT_PATH);


	public static final String IMG_PATH = String.format("%s/img",
			WidgetProvider.ROOT_PATH);
	
	public static final String NEWS_PATH = String.format("%s/news",
			WidgetProvider.CACHE_PATH);
	
	public static final String VIDEOS_PATH = String.format("%s/videos",
			WidgetProvider.CACHE_PATH);
	
	public static final String RSSITEMLOGO_PATH = String.format("%s/rssitemlogo",
			WidgetProvider.NEWS_PATH);
	
	public static final String WIKI_PATH = String.format("%s/wiki",
			WidgetProvider.CACHE_PATH);
	 
	public static final String TYPE_LIST = String.format("%s.TYPE_LIST", className);
	public static final String NEWS_TYPE_LIST = String.format("%s.NEWS_TYPE_LIST", className);
	public static final String TOP100_TYPE_LIST = String.format("%s.TOP100_TYPE_LIST", className);
	public static final String VIDEOS_TYPE_LIST = String.format("%s.VIDEO_TYPE_LIST", className);
	public static final String WIKI_TYPE_LIST = String.format("%s.WIKI_TYPE_LIST", className);
	
	String[] wrong = {
			"images",
			"sounds",
			"webkit",
			"kioskmode"
			};
	
	public static String getTypeCurrentService() {
		try {
			AppManager.getGeneralSettings().load();
			return AppManager.getGeneralSettings().getTypeCurrentService();
		} catch (Exception e) {
			LogManager.LogEA(className, e.getLocalizedMessage());
			e.printStackTrace();
			return null;
		}
	}

	public static void setTypeCurrentService(String typeCurrentService) {
		try {
			AppManager.getGeneralSettings().setTypeCurrentService(typeCurrentService);
			AppManager.getGeneralSettings().save();
		} catch (Exception e) {
			LogManager.LogEA(className, e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
	
	@Override
	public void onEnabled(final Context context) {
		LogManager.LogVA(WidgetProvider.className, ((context == null) ? context == null : context.toString()));
		super.onEnabled(context);
	}
	
	public boolean needInit(Intent intent){
		return ((!new File(CACHE_PATH).exists() || !new File(Database.DB_NAME).exists() )) && isOwnAction(intent.getAction());
		
	}
	
	public boolean isOwnAction (String action){
		return action.equals(UPDATE_ACTION) || action.equals(RESTORE_ACTION) || action.equals(SETTINGS_ACTION) || action.equals(INFO_ACTION) || action.equals(NEWS_LIST_ACTION) || action.equals(MORE_INFO_RATING_ACTION) || action.equals(TOP100_LIST_ACTION) || action.equals(VIDEOS_LIST_ACTION);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			int widgetId = intent.getIntExtra(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			LogManager.LogVA(WidgetProvider.className, 
					((context == null) ? context == null : context.toString()),
					((intent == null) ? intent == null : intent.toString()),
					widgetId);

			if(needInit(intent)){
			//	LogManager.LogVA(className, "startup", intent);
				startupWidget(intent, AppManager.getContext());
			//	return;
				//intent.setAction(WidgetProvider.UPDATE_ACTION);
			}
			LogManager.LogEA(className, "receive after startup (if else)", intent.getAction());
			
				
			if (WidgetProvider.UPDATE_ACTION.equals(intent.getAction())) {
				
			//	if(true/*AppManager.getGeneralSettings().isDownloadWithWifi() && NetworkManager.isWifiActiveNetwork(context) || !AppManager.getGeneralSettings().isDownloadWithWifi()*/){
					Intent svcIntent = new Intent(intent);
					String valueTypeList = (String) svcIntent.getExtras().get(WidgetProvider.TYPE_LIST);
					if(valueTypeList != null && valueTypeList.equals(WidgetProvider.NEWS_TYPE_LIST)){
						svcIntent.setClass(context, NewsListViewService.class);
						svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, false);
					
						new NewsRequestTask(this, context, svcIntent).execute();
					
					}
					else if(valueTypeList != null && valueTypeList.equals(WidgetProvider.TOP100_TYPE_LIST)){
						svcIntent.setClass(context, Top100ListViewService.class);
						svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, false);
						new Top100RequestTask(this, context, svcIntent).execute();
					}
					else if(valueTypeList != null && valueTypeList.equals(WidgetProvider.VIDEOS_TYPE_LIST)){
						svcIntent.setClass(context, VideosListViewService.class);
						svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, false);
						new VideosRequestTask(this, context, svcIntent).execute();
					}
					else if(valueTypeList != null && valueTypeList.equals(WidgetProvider.WIKI_TYPE_LIST)){
						svcIntent.setClass(context, WikiListViewService.class);
						svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, false);
						new WikiWidgetRequestTask(this, context, svcIntent).execute();
					}
				/*}
				else{
					Toast.makeText(context, R.string.download_disabled_mobile_internet, Toast.LENGTH_SHORT).show();
				}*/
				
			} else if (WidgetProvider.RESTORE_ACTION.equals(intent.getAction())) {
				Intent svcIntent = new Intent(intent);
				String valueTypeList = (String) svcIntent.getExtras().get(WidgetProvider.TYPE_LIST);
				if(valueTypeList != null && valueTypeList.equals(WidgetProvider.NEWS_TYPE_LIST)){
					svcIntent.setClass(context, NewsListViewService.class);
	
					svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, true);
	
					new NewsRequestTask(this, context, svcIntent).execute();
				}else if(valueTypeList != null && valueTypeList.equals(WidgetProvider.TOP100_TYPE_LIST)){
					svcIntent.setClass(context, Top100ListViewService.class);
					svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, true);

					new Top100RequestTask(this, context, svcIntent).execute();
				}else if(valueTypeList != null && valueTypeList.equals(WidgetProvider.VIDEOS_TYPE_LIST)){
					svcIntent.setClass(context, VideosListViewService.class);
	
					svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, true);
	
					new VideosRequestTask(this, context, svcIntent).execute();
				}
				else if(valueTypeList != null && valueTypeList.equals(WidgetProvider.WIKI_TYPE_LIST)){
					svcIntent.setClass(context, WikiListViewService.class);
					svcIntent.putExtra(Parser.KEY_INTENT_RESTORE, true);
					new WikiWidgetRequestTask(this, context, svcIntent).execute();
				}
			} else if (WidgetProvider.SETTINGS_ACTION
					.equals(intent.getAction())) {

				Intent i = new Intent(intent);
				i.setClass(context, ConfigurationActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
				i.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
				context.startActivity(i);
			} else if (WidgetProvider.INFO_ACTION.equals(intent.getAction())) {

				Intent i = new Intent(intent);
				i.setClass(context, AboutActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(i);
			} else if (WidgetProvider.TOP100_LIST_ACTION.equals(intent.getAction())) {
				final Intent refreshIntent = new Intent(
						AppManager.getContext(), WidgetProvider.class);
				refreshIntent
						.setAction(WidgetProvider.RESTORE_ACTION);
				refreshIntent.putExtra(
						AppWidgetManager.EXTRA_APPWIDGET_ID,
						widgetId);
				refreshIntent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.TOP100_TYPE_LIST);
				refreshIntent.setData(Uri.parse(refreshIntent
						.toUri(Intent.URI_INTENT_SCHEME)));
				
				AppManager.getContext().sendBroadcast(refreshIntent);
			
			} else if (WidgetProvider.NEWS_LIST_ACTION.equals(intent.getAction())) {
							
//				intent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.NEWS_TYPE_LIST);
//				WidgetProvider.setTypeCurrentService(WidgetProvider.NEWS_TYPE_LIST);
//				
//				intent.setClass(context, NewsListViewService.class);
//				
//				this.initNewsListWidget(intent, context);
				
				Intent refreshIntent = new Intent(
						AppManager.getContext(), WidgetProvider.class);
				refreshIntent
						.setAction(WidgetProvider.RESTORE_ACTION);
				refreshIntent.putExtra(
						AppWidgetManager.EXTRA_APPWIDGET_ID,
						widgetId);
				refreshIntent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.NEWS_TYPE_LIST);
				refreshIntent.setData(Uri.parse(refreshIntent
						.toUri(Intent.URI_INTENT_SCHEME)));
											
				AppManager.getContext().sendBroadcast(refreshIntent);
			}
				else if (WidgetProvider.VIDEOS_LIST_ACTION.equals(intent.getAction())) {
					final Intent refreshIntent = new Intent(
							AppManager.getContext(), WidgetProvider.class);
					refreshIntent
							.setAction(WidgetProvider.RESTORE_ACTION);
					refreshIntent.putExtra(
							AppWidgetManager.EXTRA_APPWIDGET_ID,
							widgetId);
					refreshIntent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.VIDEOS_TYPE_LIST);
					refreshIntent.setData(Uri.parse(refreshIntent
							.toUri(Intent.URI_INTENT_SCHEME)));
					
					AppManager.getContext().sendBroadcast(refreshIntent);
			} else if (WidgetProvider.MORE_INFO_RATING_ACTION.equals(intent.getAction())) {
				
				Intent i = new Intent(intent);
//				i.putExtra(FinderActivity.FINDER_EXTRA_SERVICE_KEY, new ClansFinderModule());
//				i.setClass(context, FinderActivity.class);
				
				i.setClass(context, RatingActivity.class);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
				i.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
				context.startActivity(i);
			}
			else if (WidgetProvider.WIKI_ACTION.equals(intent.getAction())) {
				
				
				
				Intent refreshIntent = new Intent(
						AppManager.getContext(), WidgetProvider.class);
				refreshIntent
						.setAction(WidgetProvider.RESTORE_ACTION);
				refreshIntent.putExtra(
						AppWidgetManager.EXTRA_APPWIDGET_ID,
						widgetId);
				refreshIntent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.WIKI_TYPE_LIST);
				refreshIntent.setData(Uri.parse(refreshIntent
						.toUri(Intent.URI_INTENT_SCHEME)));
											
				AppManager.getContext().sendBroadcast(refreshIntent);
				
				
				
				
				
//				
//				Intent i = new Intent(intent);
//				i.setClass(context, WikiListViewService.class);
//			//	i.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.WIKI_TYPE_LIST);
//				i.setData(Uri.parse(i
//						.toUri(Intent.URI_INTENT_SCHEME)));
//			
//				onTaskCompleted(i, context);
//				
				
//				i.setClass(context, WikiActivity.class);
//				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
//				i.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
//				context.startActivity(i);
			}
		} catch (Exception e) {
			LogManager.LogE(WidgetProvider.className, LogManager.getExecuteMethodName(),
					e.getLocalizedMessage());
			e.printStackTrace();
		}
		super.onReceive(context, intent);
	}

	@Override
	public void onUpdate(final Context ctx,
			final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
		LogManager.LogV(WidgetProvider.className, LogManager.getExecuteMethodName(),
				((ctx == null) ? ctx == null : ctx.toString()),
				((appWidgetManager == null) ? appWidgetManager == null
						: appWidgetManager.toString()), appWidgetIds

				.toString());
		for (int i = 0; i < appWidgetIds.length; i++) {
			int widgetId = appWidgetIds[i];

			LogManager.LogV(WidgetProvider.className, LogManager.getExecuteMethodName(), widgetId);

//			Intent intent = new Intent();
//			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
//			intent.putExtra(RSSParser.KEY_INTENT_RESTORE, true);
//			intent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.NEWS_TYPE_LIST);
//			intent.setClass(ctx, NewsListViewService.class);
//			this.initWidget(intent, ctx);
//			new NewsRequestTask(this, ctx, intent).execute();
			
		
				
			
			final Intent refreshIntent = new Intent(
					AppManager.getContext(), WidgetProvider.class);
//			refreshIntent
//					.setAction(WidgetProvider.VIDEOS_LIST_ACTION);
			refreshIntent.putExtra(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					widgetId);
//			AppManager.getContext().sendBroadcast(refreshIntent);
			
		
			
			WidgetProvider.setTypeCurrentService(NEWS_TYPE_LIST);
			WidgetProvider.refreshWidget(refreshIntent);
			
		}
		super.onUpdate(ctx, appWidgetManager, appWidgetIds);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		LogManager.LogV(WidgetProvider.className, LogManager.getExecuteMethodName(),
				((context == null) ? context == null : context.toString()),
				appWidgetIds.toString());
		super.onDeleted(context, appWidgetIds);
	}

	public static int getCountWidgets() {
		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(AppManager.getContext());
		return appWidgetManager.getAppWidgetIds(new ComponentName(AppManager
				.getContext(), WidgetProvider.class)).length;
	}

	@Override
	public void onDisabled(Context context) {
		LogManager.LogV(WidgetProvider.className, LogManager.getExecuteMethodName(),
				((context == null) ? context == null : context.toString()));
		super.onDisabled(context);
	}

	@Override
	public void onTaskCompleted(Intent intent, Context context) {
		LogManager.LogV(WidgetProvider.className, LogManager.getExecuteMethodName(),
				((context == null) ? context == null : context.toString()),
				((intent == null) ? intent == null : intent.toString()),
				intent.toString(), intent.getStringExtra(WidgetProvider.TYPE_LIST));
		
		if(intent.getStringExtra(WidgetProvider.TYPE_LIST).equals(WidgetProvider.NEWS_TYPE_LIST)){
			this.initNewsListWidget(intent, context);
		}
		else if(intent.getStringExtra(WidgetProvider.TYPE_LIST).equals(WidgetProvider.TOP100_TYPE_LIST)){
			this.initTop100ListWidget(intent, context);
		}
		else if(intent.getStringExtra(WidgetProvider.TYPE_LIST).equals(WidgetProvider.VIDEOS_TYPE_LIST)){
			this.initVideosListWidget(intent, context);
		}
		else if(intent.getStringExtra(WidgetProvider.TYPE_LIST).equals(WidgetProvider.WIKI_TYPE_LIST)){
			this.initWikiWidget(intent, context);
		}
	}
	public void startupWidget(final Intent intent, final Context context) throws InterruptedException{
			
			int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			AppWidgetManager manager = AppWidgetManager.getInstance(context);
			
			LogManager.LogEA(className, widgetId);
			
			RemoteViews widget = new RemoteViews(context.getPackageName(),
					R.layout.widget);
			
			RemoteViews newsListView = new RemoteViews(context.getPackageName(),
					R.layout.startup_widget_layout);
			widget.removeAllViews(R.id.content_layout);
			widget.addView(R.id.content_layout, newsListView);
			widget.setTextViewText(R.id.TextView, AppManager.getContext().getString(R.string.init_widget_please_wait));
			widget.setViewVisibility(R.id.update_progress_bar, View.VISIBLE);
			widget.setViewVisibility(R.id.update_button, View.GONE);
			
			manager.updateAppWidget(widgetId, widget);
			
			
				(new AsyncTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {
						FilesUtils.copyDirectory(WidgetProvider.class
								.getPackage().getName() /*+ "/database"*/, ROOT_PATH /*+ "/database"*/, AppManager.getContext().getAssets(), Arrays.asList(wrong));
						return null;
					}

					@Override
					protected void onPostExecute(Void result) {
						super.onPostExecute(result);
						try {
							//AppManager.getGeneralSettings().setStartupWidget(true);
							context.sendBroadcast(intent);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
					
				}).execute();
			
			
	}
	
	public void initWidget(Intent intent, Context context, RemoteViews widget){
		
		int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		
		
		/*Content chooser*/
		/*tap news button*/
		final PendingIntent newsPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.NEWS_LIST_ACTION,
						widgetId, null), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.widget_news_button, newsPendingIntent);
		
		/*click top100 button*/
		final PendingIntent top100PendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.TOP100_LIST_ACTION,
						widgetId, null), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.widget_top100_button, top100PendingIntent);
		
		/*click videos button*/
		final PendingIntent videosPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.VIDEOS_LIST_ACTION,
						widgetId, null), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.widget_videos_button, videosPendingIntent);
	
//		Bundle settingsBundle = new Bundle();
//		settingsBundle.putInt(RSSParser.KEY_INTENT_RESULT, intent.getIntExtra(RSSParser.KEY_INTENT_RESULT, RSSParser.REQUEST_COMPLETED));
		/*Control panel*/
		/*tap settings button*/
		final PendingIntent settingsPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.SETTINGS_ACTION,
						widgetId, /*settingsBundle*/null), PendingIntent.FLAG_UPDATE_CURRENT);
		
		widget.setOnClickPendingIntent(R.id.config_button,	settingsPendingIntent);
			/*tap info button*/
		final PendingIntent infoPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.INFO_ACTION,
						widgetId, null), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.info_button, infoPendingIntent);
		
		final PendingIntent wikiAction = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.WIKI_ACTION,
						widgetId, null), PendingIntent.FLAG_UPDATE_CURRENT);
			widget.setOnClickPendingIntent(R.id.widget_wiki_button, wikiAction);
		
		
	//	manager.updateAppWidget(widgetId,
		//		widget);
	}
	
	public void initNewsListWidget(Intent intent, Context context){
		
		intent.putExtra(NewsListViewService.UNIQUE_INTENT_KEY,
				new Random().nextDouble());
		intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
		
		Bundle bundle = new Bundle();
		bundle.putString(WidgetProvider.TYPE_LIST, intent.getStringExtra(WidgetProvider.TYPE_LIST));
		
		AppWidgetManager manager = AppWidgetManager.getInstance(context);

		int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);

		
		RemoteViews widget = new RemoteViews(context.getPackageName(),
				R.layout.widget);

		RemoteViews newsListView = new RemoteViews(context.getPackageName(),
				R.layout.news_list_widget_layout);
		widget.removeAllViews(R.id.content_layout);
		widget.addView(R.id.content_layout, newsListView);
		widget.setRemoteAdapter(R.id.news_list_view, intent);
		
		
			/*tap update button*/
		final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.UPDATE_ACTION,
						widgetId, bundle), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.update_button,
				refreshPendingIntent);
		
		/*News content*/
			/*tap news list item*/
		
		final PendingIntent newsListItem = PendingIntent.getActivity(context, 0, this
				.getPendingIntent(context, NewsActivity.class, null, -1, null),
				PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setPendingIntentTemplate(R.id.news_list_view, newsListItem);	
		
		
		this.initWidget(intent, context, widget);
		
		NewsRequestTask.setWidgetState(widget, intent.getIntExtra(NewsParser.KEY_INTENT_RESULT, NewsParser.REQUEST_COMPLETED));
		manager.updateAppWidget(widgetId, widget);
		
		LogManager.LogVA(className, intent);
	}	
	
	public void initWikiWidget(Intent intent, Context context){
		intent.putExtra(Top100ListViewService.UNIQUE_INTENT_KEY,
				new Random().nextDouble());
		intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
		
		Bundle bundle = new Bundle();
		bundle.putString(WidgetProvider.TYPE_LIST, intent.getStringExtra(WidgetProvider.TYPE_LIST));
		
		AppWidgetManager manager = AppWidgetManager.getInstance(context);

		int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		RemoteViews widget = new RemoteViews(context.getPackageName(),
				R.layout.widget);
		RemoteViews wikiWidgetLayout = new RemoteViews(context.getPackageName(),
				R.layout.wiki_widget_layout);
		widget.removeAllViews(R.id.content_layout);
		widget.addView(R.id.content_layout, wikiWidgetLayout);
		widget.setRemoteAdapter(R.id.gridview, intent);
			
	
		/*tap update button*/
		final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.UPDATE_ACTION,
						widgetId, bundle), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.update_button,
				refreshPendingIntent);
		
		final PendingIntent wikiListItem = PendingIntent.getActivity(context, 0, this
				.getPendingIntent(context, WikiActivity.class, null, -1, null),
				PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setPendingIntentTemplate(R.id.gridview, wikiListItem);	
		
		
		this.initWidget(intent, context, widget);
		
		WikiWidgetRequestTask.setWidgetState(widget, intent.getIntExtra(NewsParser.KEY_INTENT_RESULT, NewsParser.REQUEST_COMPLETED));
		
		manager.updateAppWidget(widgetId,
				widget);
	}
	
	public void initVideosListWidget(Intent intent, Context context){
		
		intent.putExtra(VideosListViewService.UNIQUE_INTENT_KEY,
				new Random().nextDouble());
		intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
		
		Bundle bundle = new Bundle();
		bundle.putString(WidgetProvider.TYPE_LIST, intent.getStringExtra(WidgetProvider.TYPE_LIST));
		
		AppWidgetManager manager = AppWidgetManager.getInstance(context);

		int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		//bundle.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
		
		RemoteViews widget = new RemoteViews(context.getPackageName(),
				R.layout.widget);

		RemoteViews videosListView = new RemoteViews(context.getPackageName(),
				R.layout.videos_list_widget_layout);
		
		widget.removeAllViews(R.id.content_layout);
		widget.addView(R.id.content_layout, videosListView);
		widget.setRemoteAdapter(R.id.videos_list_widget_layout_videos_listview, intent);
		
		//widget.setScrollPosition(R.id.videos_list_widget_layout_videos_listview, 20);
		
		
			/*tap update button*/
		final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.UPDATE_ACTION,
						widgetId, bundle), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.update_button,
				refreshPendingIntent);
		
		/*News content*/
			/*tap news list item*/
		
		final PendingIntent videosListItem = PendingIntent.getActivity(context, 0, this
				.getPendingIntent(context, YouTubeVideoPlayerActivity.class, null, -1, null),
				PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setPendingIntentTemplate(R.id.videos_list_widget_layout_videos_listview, videosListItem);	
		
		
		this.initWidget(intent, context, widget);
		
		VideosRequestTask.setWidgetState(widget, intent.getIntExtra(VideosParser.KEY_INTENT_RESULT, VideosParser.REQUEST_COMPLETED));
		manager.updateAppWidget(widgetId, widget);
		
		LogManager.LogVA(className, intent);
	}	
	
	public void initTop100ListWidget(Intent intent, Context context){
		
		intent.putExtra(Top100ListViewService.UNIQUE_INTENT_KEY,
				new Random().nextDouble());
		intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
		
		Bundle bundle = new Bundle();
		bundle.putString(WidgetProvider.TYPE_LIST, intent.getStringExtra(WidgetProvider.TYPE_LIST));
		
		AppWidgetManager manager = AppWidgetManager.getInstance(context);

		int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		RemoteViews widget = new RemoteViews(context.getPackageName(),
				R.layout.widget);
		RemoteViews top100ListView = new RemoteViews(context.getPackageName(),
				R.layout.top100_list_widget_layout);
		widget.removeAllViews(R.id.content_layout);
		widget.addView(R.id.content_layout, top100ListView);
		widget.setRemoteAdapter(R.id.clans_list_View, intent);
//		
		/*tap update button*/
		final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(
				context, 0, this.getPendingIntent(context,
						WidgetProvider.class, WidgetProvider.UPDATE_ACTION,
						widgetId, bundle), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.update_button,
				refreshPendingIntent);
				
		/*Clans content*/
			/*tap clans list item*/
		final PendingIntent moreInfoRatingAction = PendingIntent.getBroadcast(
			context, 0, this.getPendingIntent(context,
					WidgetProvider.class, WidgetProvider.MORE_INFO_RATING_ACTION,
					widgetId, null), PendingIntent.FLAG_UPDATE_CURRENT);
		widget.setOnClickPendingIntent(R.id.more_ratings, moreInfoRatingAction);
		
		widget.setTextViewText(R.id.more_ratings, AppManager.getContext().getString(R.string.moreInfo));
		
		this.initWidget(intent, context, widget);
		
		Top100RequestTask.setWidgetState(widget, intent.getIntExtra(Parser.KEY_INTENT_RESULT, Parser.REQUEST_COMPLETED));
		
		manager.updateAppWidget(widgetId,
				widget);
			
		LogManager.LogV(className, LogManager.getExecuteMethodName(), intent);
	}

	public Intent getPendingIntent(Context context, Class<?> cls,
			String action, int widgetId, Bundle bundle) {
		Intent intent = new Intent(context, cls);

		if (action != null) {
			intent.setAction(action);
		}
		if (widgetId != -1) {
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
		}
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
		return intent;
	}
	
	public static void refreshWidget(Intent intent){
		try{
			int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,-1);
			if(widgetId == -1) throw new Exception("bug - please add widget id to intent");
			
			Intent refreshIntent = new Intent(
					AppManager.getContext(), WidgetProvider.class);
			refreshIntent
					.setAction(WidgetProvider.RESTORE_ACTION);
			refreshIntent.putExtra(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					widgetId);
			refreshIntent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.getTypeCurrentService());
			refreshIntent.setData(Uri.parse(refreshIntent
					.toUri(Intent.URI_INTENT_SCHEME)));
										
			AppManager.getContext().sendBroadcast(refreshIntent);
		}
		catch(Exception e){
			LogManager.LogEA(className, e.getLocalizedMessage());
			e.printStackTrace();
		}

	}
}